//
//  VRDiscoverViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/11.
//

import UIKit

class VRDiscoverViewController: VRBaseViewController {

    private var topView = VRDiscoverTopView()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 410
        tableView.register(VRDiscoverCell.self)
        return tableView
    }()
    private var type = 1 //1.recommend 2.following
    private let emptyView = VRContentEmptyView()
    
    var recommendArr:[InfoUtil.room] = []
    var followArr:[InfoUtil.room] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            topView
            tableView
            emptyView
        }
        
        topView.leading(0).trailing(0).top(.statusHeight).height(44)
        tableView.leading(0).trailing(0).bottom(.tabBar_height).top(.navi_height)
        emptyView.top(.navi_height + 100).leading(0).trailing(0).height(180)
        emptyView.isHidden = true
        loadData()
        topView.recommendBlock = { [weak self] in
            self?.type = 1
            self?.loadData()
        }
        topView.followingBlock = { [weak self] in
            self?.type = 2
            self?.loadData()
        }
    }

    private func loadData(){

        self.recommendArr.removeAll()
        self.followArr.removeAll()
        
        if type == 1 {
            
            RT.getRoomList { list in
                for item in list{
                    if item?.info4 == "___*VoiceRoom" && item?.info0 == "VoiceRoom___*Story" {
                        self.recommendArr.append(item!)
                    }
                }
                
                RT.blockListWith { response in
                    for data in response
                    {
                        self.recommendArr.removeAll(where: {$0.id == data?.id})
                    }
                    self.tableView.reloadData()
                 }
            }
        }else {
            
            RT.getMyCaredHouseList { list in
                for item in list{
                    if item?.info4 == "___*VoiceRoom" && item?.info0 == "VoiceRoom___*Story" {
                        self.followArr.append(item!)
                    }
                }
                
                RT.blockListWith { response in
                    for data in response
                    {
                        self.followArr.removeAll(where: {$0.id == data?.id})
                    }
//                    self.loadFollowingStory()
                    self.tableView.reloadData()
                 }
            }
        }
    }
    
    private func loadFollowingStory(){
        RT.FriendListAction { response in
            for data in response
            {
                self.followArr.removeAll(where: {$0.id != data?.id})
            }
            self.tableView.reloadData()
        }
    }
}

extension VRDiscoverViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if type == 1 {
            if recommendArr.count == 0 {
                emptyView.isHidden = false
            }else{
                emptyView.isHidden = true
            }
            return recommendArr.count
        }else{
            if followArr.count == 0 {
                emptyView.isHidden = false
            }else{
                emptyView.isHidden = true
            }
            return followArr.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.cell(VRDiscoverCell.self, indexPath: indexPath)

        if type == 1 {
            cell.roomModel = recommendArr[indexPath.row]
        }else{
            cell.roomModel = followArr[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let detailVC = VRStoryDetailViewController()
        if type == 1 {
            detailVC.itemModel = recommendArr[indexPath.row]
        }else{
            detailVC.itemModel = followArr[indexPath.row]
        }
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
}
